﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Assertions;

[Serializable]
public class ShaderKeywords : IEnumerable<int>, IEquatable<ShaderKeywords>, IEnumerable
{
    private const int defaultValue = 32;
    private static readonly string[] keywords_array = new string[32];
    [SerializeField]
    private int keywords;


    private struct ShaderKeywordsData : IEnumerator, IDisposable, IEnumerator<int>
    {
        int value;
        int index;

        public ShaderKeywordsData(int keywords)
        {
            this.value = keywords;
            this.index = -1;
        }
        object IEnumerator.Current
        {
            get
            {
                return this.Current;
            }
        }

        public int Current
        {
            get
            {
                return this.index;
            }
        }

        public bool MoveNext()
        {
            this.index++;
            int num = 32;
            for (int i = this.index; i < num; i++)
            {
                if ((this.value & 1 << i) != 0)
                {
                    this.index = i;
                    return true;
                }
            }
            return false;
        }

        public void Reset()
        {
            this.index = -1;
        }

        public void Dispose()
        {
        }

    }

    public bool Equals(ShaderKeywords other)
    {
        return this.keywords == other.keywords;
    }

    public IEnumerator<int> GetEnumerator()
    {
        return new ShaderKeywordsData(this.keywords);
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return this.GetEnumerator();
    }
    public override int GetHashCode()
    {
        return this.keywords.GetHashCode();
    }
    public bool IsEmpty
    {
        get
        {
            return this.keywords == 0;
        }
    }


    public static void SetKeywordName(int keyword, string name)
    {
        Assert.IsTrue(keyword >= 0 && keyword < 32);
        keywords_array[keyword] = name;
    }

    public static string GetKeywordName(int keyword)
    {
        Assert.IsTrue(keyword >= 0 && keyword < 32);
        return keywords_array[keyword];
    }

    public void SetKeyword(int keyword)
    {
        Assert.IsTrue(keyword >= 0 && keyword < 32);
        this.keywords |= 1 << keyword;
    }

    public void UnsetKeyword(int keyword)
    {
        Assert.IsTrue(keyword >= 0 && keyword < 32);
        this.keywords &= ~(1 << keyword);
    }
    public void ToggleKeyword(int keyword)
    {
        Assert.IsTrue(keyword >= 0 && keyword < 32);
        this.keywords ^= 1 << keyword;
    }
    public void Merge(ShaderKeywords keywords)
    {
        this.keywords |= keywords.keywords;
    }
    public bool HasKeyword(int keyword)
    {
        Assert.IsTrue(keyword > 0 && keyword < 32);
        return (this.keywords & 1 << keyword) != 0;
    }







}
